package br.com.ac.fatureweb.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.ac.fatureweb.model.vo.MonitoramentoVO;
import br.com.ac.fatureweb.service.NgcMonitoramentoBean;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MonitorarMapsServlet extends HttpServlet
{

	private static final long serialVersionUID = 72743959717785163L;
	
	@Inject
	private NgcMonitoramentoBean ngcMonitoramentoBean;

	@Override
	protected void doPost(final HttpServletRequest req,
			final HttpServletResponse resp) throws ServletException, IOException
	{
		PrintWriter out = null;

		try
		{
			resp.setContentType("application/json; charset=UTF-8");

			final BufferedReader br = new BufferedReader(new InputStreamReader(
					req.getInputStream()));
			
			String filtroJson = "";
			filtroJson = br.readLine();

			final MonitoramentoVO filtroVO = new Gson().fromJson(filtroJson, new TypeToken<MonitoramentoVO>()
					{
					}.getType());

			if ( filtroVO != null )
			{
				final List<MonitoramentoVO> result = ngcMonitoramentoBean
						.consultarMonitoramento(filtroVO);
				
				out = resp.getWriter();
				
				out.print(new Gson().toJson(result));
			}
		}
		catch (final Exception e)
		{
			throw new IllegalArgumentException("Erro no MonitorarMapsServlet");
		}
		finally
		{
			if ( out != null )
			{
				out.close();
			}
		}
	}
}